<?php

namespace App\Http\Middleware;

use App\Models\User;
use Closure;
use Illuminate\Http\Request;

class ApiToken
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $token = $request->header('X-Token', '');
        if ($token) {
            $user = User::where('remember_token', $token)->first();
            if ($user) {
                $request->offsetSet('TOKEN_USER', $user);
            }
        }
        return $next($request);
    }
}
